//
//  DYJingquYoulanCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/4.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

//DYJingquYoulanSubCell

import UIKit

class DYJingquYoulanCell: SYBaseCell , UICollectionViewDelegate, UICollectionViewDataSource{
    
    var collectionView: UICollectionView!
    var model: DYJingquDetailModel!

    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(hex: 0x367FF6)
        v.layer.cornerRadius = 2
        return v
    }()
    
    var sepView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1E1E1E)
        v.font = 17.autoBoldFontSize()
        v.text = "景区游览"
        return v
    }()
    
    let chakan: UIButton = {
        let v: UIButton  = UIButton()
        v.setTitle("全部 >", for: .normal)
        v.setTitleColor(UIColor(hex: 0x5B5B5B), for: .normal)
        v.titleLabel?.font = 14.autoFontSize()
        v.layer.cornerRadius = 13.5
        v.backgroundColor = UIColor.white
        return v
    }()

    public func configureView(_ model: DYJingquDetailModel) {
        self.model = model
        self.collectionView.reloadData()
    }
    
    override func initViews() {
        chakan.addTarget(self, action: #selector(chakanAction), for: .touchUpInside)
        setCollectionView()
        contentView.addSubview(nameLb)
        contentView.addSubview(bgView)
        contentView.addSubview(chakan)
        contentView.addSubview(collectionView)
        contentView.addSubview(chakan)
        contentView.addSubview(sepView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(14.5))
            make.width.equalTo(4.0.autoWidth())
            make.height.equalTo(15.0.autoWidth())
        }
        nameLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(bgView)
            make.left.equalTo(bgView.snp.right).offset(autoWidth(7.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(16.0.autoWidth())
        }
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(bgView.snp.bottom).offset(autoWidth(15.0))
            make.left.right.equalToSuperview()
            make.height.equalTo(125.0.autoWidth())
        }
        chakan.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameLb).offset(3.0.autoWidth())
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(30.0.autoWidth())
        }
        sepView.snp.makeConstraints { (make) in
            make.top.equalTo(collectionView.snp.bottom).offset(16.0.autoWidth())
            make.left.right.equalToSuperview()
            make.height.equalTo(10.0.autoWidth())
            make.bottom.equalToSuperview()
        }
    }
    
    func setCollectionView() {
        
            
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        let itemWidth = 145.0
        layout.itemSize = CGSize(width: itemWidth, height: 125)
//        layout.minimumLineSpacing = 10
//        layout.minimumInteritemSpacing = 10
        layout.scrollDirection = .horizontal
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(DYJingquYoulanSubCell.self, forCellWithReuseIdentifier: NSStringFromClass(DYJingquYoulanSubCell.self))
        self.addSubview(self.collectionView)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if let m = model, let c = m.tourScenicVisits?.count {
            return c
        }
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: DYJingquYoulanSubCell? = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(DYJingquYoulanSubCell.self), for: indexPath) as? DYJingquYoulanSubCell
        if let m = model, let l = m.tourScenicVisits?[indexPath.row] {
            cell?.configureView(l)
        }
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let m = model, let l = m.tourScenicVisits?[indexPath.row] {
            let vc = DYJingquJianjieViewController(model: l)
            self.currentVC.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    
    @objc func chakanAction() {
        if let m = model {
            let vc = DYJingQuListViewController(id: m.id ?? "")
            self.currentVC.navigationController?.pushViewController(vc, animated: true)
        }
    }
}

